// src/http/axios.js
import axios from 'axios'

 
axios.defaults.timeout = 10000 // 请求超时时间
 
const Service = axios.create({
    baseURL: '/',
})
 
// 修复请求拦截器结构
Service.interceptors.request.use(
  config => {
    if (localStorage.getItem("token")) {
      config.headers.Authorization = 'Bearer ' + localStorage.getItem('token');
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

// 修复响应拦截器结构
Service.interceptors.response.use(
  response => {
    // 添加业务状态码检查
    if (response.data.code && response.data.code !== 200) {
      return Promise.reject(response);
    }
    return response.data;
  },
  error => {
    // 统一错误处理
    if (error.response) {
      switch (error.response.status) {
        case 401:
          localStorage.removeItem('token');
          window.location.href = '/login';
          break;
        default:
          console.error('请求错误:', error);
      }
    }
    return Promise.reject(error);
  }
);
export default Service;